package transactions

import (
	"Salary/database"
	"Salary/model"
	"time"
)

type PaydayTransaction struct {
	payDate   time.Time
	paychecks map[int]*model.Paycheck
}

func NewPaydayTransaction(t time.Time) *PaydayTransaction {
	return &PaydayTransaction{
		payDate:   t,
		paychecks: make(map[int]*model.Paycheck),
	}
}

func (pt *PaydayTransaction) Execute() {
	employee_ids := database.GetPayrollDatabaseInstance().GetAllEmployees()
	for _, e := range employee_ids {
		if e.IsPayDate(pt.payDate) {
			pc := model.NewPaycheck(e.GetPayPeriodStartDate(pt.payDate), pt.payDate)
			pt.paychecks[e.EmployeeId] = pc
			e.Payday(pc)
		}
	}
}

func (pt *PaydayTransaction) GetPayCheck(employee_id int) *model.Paycheck {
	paycheck, ok := pt.paychecks[employee_id]
	if ok {
		return paycheck
	} else {
		return nil
	}
}
func (pt *PaydayTransaction) GetPayCheckCount() int {
	return len(pt.paychecks)
}
